home *** CD-ROM | disk | FTP | other *** search
/ All for Cell Phones: Sony Ericsson / Sony-Ericsson 2004.iso / Java / Chess / Fly_Chess.jar / LogonForm.class (.txt) < prev    next >
Encoding:
Java Class File  |  2001-08-23  |  4.9 KB  |  160 lines

  1. import javax.microedition.lcdui.Command;
  2. import javax.microedition.lcdui.CommandListener;
  3. import javax.microedition.lcdui.Display;
  4. import javax.microedition.lcdui.Displayable;
  5. import javax.microedition.lcdui.Form;
  6. import javax.microedition.lcdui.TextField;
  7. import javax.microedition.rms.RecordStore;
  8.  
  9. public class LogonForm implements CommandListener, ChessDisplayable, ServletResponseReceiver {
  10.    private String servleturl = null;
  11.    private String chessengineurl = null;
  12.    private TextField username = null;
  13.    private TextField password = null;
  14.    private boolean responsereceived = false;
  15.    private String response = null;
  16.    private ChessLogic chesslogic = null;
  17.    private ChessDisplayable chessreturn = null;
  18.    private Form form;
  19.    private Command OKCommand;
  20.    private Command backCommand;
  21.  
  22.    public LogonForm(ChessLogic var1, ChessDisplayable var2) {
  23.       this.chesslogic = var1;
  24.       this.chessreturn = var2;
  25.       this.loadURLs();
  26.       this.initialize();
  27.    }
  28.  
  29.    public void commandAction(Command var1, Displayable var2) {
  30.       if (var1 == this.OKCommand) {
  31.          MessageForm var3;
  32.          if (this.logon()) {
  33.             var3 = new MessageForm("Logon OK", CU.removeFirstTokens(this.response, 1), this.chessreturn);
  34.          } else {
  35.             var3 = new MessageForm("Logon failed", CU.removeFirstTokens(this.response, 1), this.chessreturn);
  36.          }
  37.  
  38.          var3.makeCurrent(CU.getDisplay());
  39.       }
  40.  
  41.       if (var1 == this.backCommand) {
  42.          this.chessreturn.makeCurrent(CU.getDisplay());
  43.       }
  44.  
  45.    }
  46.  
  47.    private void initialize() {
  48.       this.OKCommand = new Command("OK", 4, 1);
  49.       this.backCommand = new Command("Back", 2, 1);
  50.       this.form = new Form("Logon to");
  51.  
  52.       String var1;
  53.       String var2;
  54.       try {
  55.          Object var3 = null;
  56.          RecordStore var5 = RecordStore.openRecordStore("Chess", false);
  57.          if (var5.getRecord(3) == null) {
  58.             var1 = "";
  59.          } else {
  60.             var1 = new String(var5.getRecord(3));
  61.          }
  62.  
  63.          if (var5.getRecord(4) == null) {
  64.             var2 = "";
  65.          } else {
  66.             var2 = new String(var5.getRecord(4));
  67.          }
  68.  
  69.          var5.closeRecordStore();
  70.       } catch (Exception var4) {
  71.          CU.shout("RS user,pass load error: " + ((Throwable)var4).getMessage());
  72.          var1 = "";
  73.          var2 = "";
  74.       }
  75.  
  76.       this.form.append("Please provide username and password \n(A password is only required for a registered logon)");
  77.       this.username = new TextField("User-name: ", var1, 20, 0);
  78.       this.password = new TextField("Password: ", var2, 20, 0);
  79.       this.form.append(this.username);
  80.       this.form.append(this.password);
  81.       this.form.addCommand(this.OKCommand);
  82.       this.form.addCommand(this.backCommand);
  83.    }
  84.  
  85.    private void loadURLs() {
  86.       try {
  87.          Object var1 = null;
  88.          RecordStore var3 = RecordStore.openRecordStore("Chess", false);
  89.          this.servleturl = new String(var3.getRecord(1));
  90.          this.chessengineurl = new String(var3.getRecord(2));
  91.          var3.closeRecordStore();
  92.       } catch (Exception var2) {
  93.          CU.shout("RS load: " + ((Throwable)var2).getMessage());
  94.          this.servleturl = "http://localhost:8080/requestservlet";
  95.          this.chessengineurl = "chess.unix-ag.uni-kl.de";
  96.       }
  97.  
  98.    }
  99.  
  100.    private boolean logon() {
  101.       String var1 = this.username.getString();
  102.       String var2 = this.password.getString();
  103.  
  104.       try {
  105.          Object var3 = null;
  106.          RecordStore var7 = RecordStore.openRecordStore("Chess", false);
  107.          var7.setRecord(3, var1.getBytes(), 0, var1.length());
  108.          var7.setRecord(4, var2.getBytes(), 0, var2.length());
  109.          var7.closeRecordStore();
  110.       } catch (Exception var6) {
  111.          CU.shout("Store UP exception: " + ((Throwable)var6).getMessage());
  112.       }
  113.  
  114.       CU.setUserName(var1);
  115.       if (var1.length() < 3) {
  116.          this.response = "logon Username must contain at least three characters";
  117.          return false;
  118.       } else {
  119.          this.form.setTitle("Performing logon");
  120.          ChessGame var8 = new ChessGame(this.chesslogic, this.servleturl);
  121.          var8.setServletRedirection(this);
  122.          if (!var2.equals("")) {
  123.             var8.doPost("logon " + this.chessengineurl + " registered " + var1 + " " + var2, false);
  124.          } else {
  125.             var8.doPost("logon " + this.chessengineurl + " unregistered " + var1, false);
  126.          }
  127.  
  128.          int var4;
  129.          for(var4 = 0; !this.responsereceived && var4 < 300; ++var4) {
  130.             try {
  131.                Thread.sleep(500L);
  132.             } catch (Exception var5) {
  133.             }
  134.          }
  135.  
  136.          if (var4 > 295) {
  137.             this.response = "logon Logon timed out after 2 and a half minutes, please try again later";
  138.             return false;
  139.          } else if (CU.containsExpr(this.response, "logon Logon failed")) {
  140.             this.chesslogic.setChessGame((ChessGame)null);
  141.             return false;
  142.          } else {
  143.             this.chesslogic.setChessGame(var8);
  144.             var8.setServletRedirection(this.chesslogic);
  145.             return true;
  146.          }
  147.       }
  148.    }
  149.  
  150.    public void makeCurrent(Display var1) {
  151.       var1.setCurrent(this.form);
  152.       this.form.setCommandListener(this);
  153.    }
  154.  
  155.    public void receiveServletResponse(String var1) {
  156.       this.response = var1;
  157.       this.responsereceived = true;
  158.    }
  159. }
  160.